package dao

import (
	"fmt"
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/frame/g"
)

var T_wx_oa_account = t_wx_oa_account{}

type t_wx_oa_account struct {
}

func (a *t_wx_oa_account) GetByUnionid(unionid string) (item gdb.Map,err error) {
	db:=g.DB("default")
	model := db.Model("t_wx_oa_account")
	result,error:= model.Where(fmt.Sprintf(" unionid='%s' ",unionid)).All()
	if error!=nil{
		g.Log().Line(true).Error(err)
		err = error
	}else{
		list := result.List()
		if len(list)>0{
			item = list[0]
		}else{
			item = nil
		}
	}
	return item,err
}

func (a *t_wx_oa_account) ExistOpenId(openid string) (exist bool,err error) {
	db:=g.DB("default")
	model := db.Model("t_wx_oa_account")
	res,err:= model.Where(fmt.Sprintf(" openid='%s' ",openid)).Count()
	if res>0{
		exist = true
	}else{
		exist =false
	}
	return exist,err
}

func (a *t_wx_oa_account) ExistUnionID(unionid string) (exist bool,err error) {
	db:=g.DB("default")
	model := db.Model("t_wx_oa_account")
	res,err:= model.Where(fmt.Sprintf(" unionid='%s' ",unionid)).Count()
	if res>0{
		exist = true
	}else{
		exist =false
	}
	return exist,err
}

func (a *t_wx_oa_account) GetList(where ...string) (list gdb.List,err error) {
	db:=g.DB("default")
	model := db.Model("t_wx_oa_account")
	if len(where)==0{
	}else {
		model = model.Where(where[0])
	}
	res,err:=model.All()
	return res.List(),err
}

func (a *t_wx_oa_account) SaveList(list gdb.List) (isOk bool,err error) {
	db:=g.DB("default")
	_,err = db.Model("t_wx_oa_account").Data(list).Save()
	if err!=nil{
		return false, err
	}else{
		return true,nil
	}
}

func (a *t_wx_oa_account) Save(item gdb.Map) (isOk bool,err error) {
	db:=g.DB("default")
	_,err = db.Model("t_wx_oa_account").Data(item).Save()
	if err!=nil{
		return false, err
	}else{
		return true,nil
	}
}

func (a *t_wx_oa_account) Delete(where ...string) (isOk bool,err error) {
	db:=g.DB("default")
	model := db.Model("t_wx_oa_account")
	_,err = model.Delete(where)
	if err!=nil{
		return false, err
	}else{
		return true,nil
	}
}